package com.auerdi.dialog;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatDialogFragment;
import android.support.v7.widget.CardView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.widget.LinearLayout;

import com.auerdi.AnimationUtils.AnimationUtils;
import com.auerdi.R;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by highsixty on 2018/3/15.
 * mail  gaolulin@sunmi.com
 */

public class PayWayDialog extends AppCompatDialogFragment {


    //    private static String mString;
    Unbinder unbinder;
    @BindView(R.id.container)
    LinearLayout container;
    @BindView(R.id.recy)
    RecyclerView recy;
    @BindView(R.id.pay)
    CardView pay;

    private int nowSelect = 0;
    private List<PayWay> payWayList = new ArrayList<>();


    public PayWayDialog() {
        super();
    }

    public static PayWayDialog newInstance(String ordername, PayWayListener payWayListener) {
//        mString = product;

        PayWayDialog fragment = new PayWayDialog();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        fragment.setListener(payWayListener);
        return fragment;
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();

        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(true);


        Window window = getDialog().getWindow();
        WindowManager.LayoutParams windowParams = window.getAttributes();
//        windowParams.dimAmount = 0.0f;

        window.setAttributes(windowParams);
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_payway_layout, container, false);
        unbinder = ButterKnife.bind(this, view);
        AnimationUtils.slideToUp(view);

        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        payWayList.add(new PayWay("微信支付", true, R.mipmap.wechat));

        recy.setLayoutManager(new GridLayoutManager(getActivity(), 1) {


        });
        recy.setHasFixedSize(true);


        recy.setAdapter(new CommonAdapter<PayWay>(getActivity(), R.layout.item_pay_way, payWayList) {
            @Override
            protected void convert(ViewHolder viewHolder, final PayWay payWay, int position) {


                viewHolder.setText(R.id.title, payWay.title + "");
                viewHolder.setImageResource(R.id.img, payWay.imageId);
                viewHolder.setVisible(R.id.okok, payWay.isSelect);


                viewHolder.getConvertView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        nowSelect = position;
                        notifyDataSetChanged();

                    }
                });


            }
        });


        pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();

                listener.onPayWay(nowSelect);
            }
        });

        container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                AnimationUtils.slideToDown(view, new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        dismiss();

                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });


            }
        });


    }


    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onDestroyView() {


        super.onDestroyView();
        unbinder.unbind();
    }

    private class PayWay {


        public PayWay(String title, Boolean isSelect, int imageId) {
            this.title = title;
            this.isSelect = isSelect;
            this.imageId = imageId;
        }

        public String title;
        public Boolean isSelect;
        public int imageId;


    }


    private PayWayListener listener = null;

    public void setListener(PayWayListener listener) {
        this.listener = listener;
    }

    public interface PayWayListener {
        void onPayWay(Integer num);
    }
}
